<?php 

   /**
    *
    * FILE:          cieListQuery.php
    * LOCATION:      /root
    * REFERENCES:    admin.php
    *                cieSearch.php
    *                cieEdit.php
    * FUNCTION:      displays a table of consumers that are
    *                returned from the database as a resultset
    *                based on search parameters from cieSearch.php
    * DEPENDENCIES:  db_connect.php 
    * 
    * --> CREATED 10-18-07
    * ==============================================================================
    * 
    * @version $Id: admin.php,v 0.1 2007/09/3
    * @copyright Copyright (c) none
    * @author Peter Holcomb
    * @license http://opensource.org/licenses/gpl-license.php GNU General 
    * Public License Version 2 (GPL v2)
    * 
    * ==============================================================================
    */
    
        //connect to database
	include('includes/db_connect.php');
	
	//what were the search parameters sent by user?
	$searchStr  = $_POST['searchStr'];
	$searchAttr = $_POST['searchAttr'];
	


	//echo "Search String: ".$searchStr." Search Attribute: ".$searchAttr."<br/>";

       

        //set query request to match correct database field
	if($searchAttr=='lName') {

                //query by last name
		$col = 'c.last_name';

	} else if($searchAttr=='fName') {

                //query by first name
		$col = 'c.first_name';

	} else if($searchAttr=='city') {

                //query by city
		$col = 'c.city';

	} else if($searchAttr=='phone') {

                //query by phone number
		$col = 'c.phone';

	}else if($searchAttr=='false') {

                //no query option chosen
		$col = null;
	}


	if($col != null) {
	  
                 $query = "SELECT c.id, 
                                  c.last_name, 
                                  c.first_name, 
                                  c.city, 
                                  c.phone, 
                                  c.address, 
                                  c.zip, 
                                  c.phone, 
                                  ls.name as state 
		            FROM       consumer c, 
                                       lst_state ls 
                            WHERE  ".$col." 
                            LIKE \"%".$searchStr."%\" 
                            and    c.state_id = ls.id 
                            order by c.last_name asc;";
             
	         //echo $query."<br/>";
	

	         $result = mysql_query($query);

                 $num_rows = mysql_num_rows($result);

                 if($num_rows < 1){
?>
                    
                     <center><h2>There were no results that matched your search request.</h2></center>

                     <!-- USER HAS OPTION TO CREATE NEW CONSUMER CASE FILE -->
                     <p style="text-align:right;">
	                 <a style="border-top:1px #ccc dotted" href="admin.php?action=cieedit&id=0"><strong>Create New Case File</strong></a>
                     </p>
<?php
                      exit();
                 }


	         $i  = 0;
?>


                  <!-- THIS TABLE EDITED ON 10-3-07 TO RENDER NEW TABLE STYLE -->
                  <!-- SET UP RESULTSET TABLE -->
                  <table class="table1">
                     <thead> 
                         <tr>
                            <th>Full Name</th>
                            <th>Address</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Zip</th>
                            <th>Phone</th>
                          </tr>
                      </thead>
                      <tbody>

<?php 

                  /*
                      we want to alternate the style of each row, so set 
                      style attribute to odd or even based on query resultset 
                      given by database
                   */
   
      while($row=mysql_fetch_array($result)) { 
	  
         if($i%2==0) {
		 
            echo "<tr class='even'>";
	
         }else {
	    
            echo "<tr class='odd'>";
	      
         }
?>
      
         <!-- USER CAN CLICK ON FIRST OR LAST NAME TO VIEW CONSUMER CASE FILE INFO -->
         <!-- changed to fullname on 10-3-07 -->
         <td><a href="admin.php?action=cieedit&id=<?php echo $row["id"]; ?>">
                <?php echo $row["last_name"].", ".$row["first_name"]; ?></a>
         </td>
         <td><?php echo $row["address"]; ?></td>
         <td><?php echo $row["city"]; ?></td>
         <td><?php echo $row["state"]; ?></td>
         <td><?php echo $row["zip"]; ?></td>
         <td><?php echo $row["phone"]; ?></td>
         </tr>
         </tbody>
  
<?php 

         $i++; 
      
      }//end result set while loop 
   
?>
   </table>
   <!-- END RESULTSET TABLE -->

<?php 

   } else { 
    
?>

      <center><h1>Sorry, no results were found</h1></center>


<?php 

   } 
   
?>

   <br/>

<!-- USER HAS OPTION TO CREATE NEW CONSUMER CASE FILE -->
<p style="text-align:right;">
	<a style="border-top:1px #ccc dotted" href="admin.php?action=cieedit&id=0"><strong>Create New Case File</strong></a>
</p>